import 'dart:async';

import 'package:ceramics/extensions/ab_widget_extensions.dart';
import 'package:ceramics/styles/ab_styles.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'ab_login_page.dart';

class ABGuidePage extends StatefulWidget {

  const ABGuidePage({super.key});

  @override
  State<ABGuidePage> createState() => _ABGuidePageState();
}

class _ABGuidePageState extends State<ABGuidePage> {
  final _swiperController = SwiperController();
  final images = ['images/goods/C/2/10005.jpg','images/goods/E/2/10010.jpg','images/goods/F/1/10008.jpg'];
  final titles = ['Find the right adventure partner to explore your rail journey with you.',
    'Interact with adventurers from around the world!',
    'Come join us and start your adventure in the world of railways!'
  ];
  Timer? _timer;
  @override
  void initState() {
    super.initState();
    _swiperController.addListener(() {
      if (_swiperController.index == 3) {
        Get.offAll(()=>const ABLoginPage());
      }
    });
    _timer = Timer.periodic(const Duration(seconds: 2), (timer) {
      if (_swiperController.index == 2) {
        Get.offAll(()=>const ABLoginPage());
        var sp = SharedPreferences.getInstance();
        sp.then((value) => value.setBool('firstOpen', false));
      } else {
        _swiperController.next();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Swiper(itemCount: images.length + 1,itemBuilder: (ctx,idx){
      if (idx < images.length) return _buildImagePage(idx);
      return const ABLoginPage();
    },pagination: const SwiperPagination(margin: EdgeInsets.only(bottom: 50),builder: DotSwiperPaginationBuilder(color: Colors.grey,activeColor: Colors.black)),
      controller: _swiperController,
      loop: false,
      onIndexChanged: (idx) async {
        if (idx == images.length) {
          Get.offAll(()=>const ABLoginPage());
          var sp = await SharedPreferences.getInstance();
          sp.setBool('firstOpen', false);
        }
      },
    );
  }

  Widget _buildImagePage(int idx) {
    return Column(
      children: [
        SizedBox(
            width: double.infinity,
            child: Transform.scale(
                scale: 1.11,
                child: Image.asset(images[idx],fit: BoxFit.cover))).expand(),
        CustomPaint(
          painter: ABGuideArcPainter(),
          child: ClipPath(
            clipper: ABGuideArcClipper(),
            child: Container(
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('images/login_bg.webp'),
                  fit: BoxFit.cover,
                  alignment: Alignment.topCenter
                ),
              ),
              height: 300,
              child: Center(child: Text(titles[idx],textAlign: TextAlign.center,style: ABStyles.tsDarkBlack15)).padSymmetric(horizontal: 20),
            ),
          ),
        )
      ],
    );
  }
}

class ABGuideArcClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    var path = Path();
    path.moveTo(0, 30);
    path.quadraticBezierTo(size.width / 2, -30, size.width, 30);
    path.lineTo(size.width, size.height);
    path.lineTo(0, size.height);
    return path;
  }

  @override
  bool shouldReclip(covariant CustomClipper<Path> oldClipper) {
    return false;
  }
}

class ABGuideArcPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    var paint = Paint()
      ..color = Colors.black
      ..strokeWidth = 3
      ..style = PaintingStyle.stroke;
    var path = Path();
    path.moveTo(0, 30);
    path.quadraticBezierTo(size.width / 2, -30, size.width, 30);
    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}